
var xmlHttp;
var board;
var customer;
var business;

function onBoardChange(value, callback) {
	if(xmlHttp = GetXmlHttpObject()){
		board = value;
		var url = "query.php";
		url = url + "?board=" + board;
		url = url + "&type=customer";
		url = url + "&sid=" + Math.random();
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
				document.getElementById("project").innerHTML = "";
				
				var sub = xmlHttp.responseText.split('|');
				var id = sub[0].split(',');
				var text = sub[1].split(',');
				var select_customer = document.getElementById("select_customer");
				select_customer.options.length=0;
				var option = document.createElement("option");
				option.value = "empty";
				option.text = "请选择客户";
				select_customer.add(option);
				
				var select_business = document.getElementById("select_business");
				select_business.options.length = 0;
				var option = document.createElement("option");
				option.value = "empty";
				option.text = "请选择客户";
				select_business.add(option);
				
				for(i=0;i<id.length;i++){
					var option = document.createElement("option");
					option.value = id[i];
					option.text = text[i];
					select_customer.add(option);
				}
				$('#updateProject').hide();
			}
		};
		xmlHttp.open("GET", url, true);
		xmlHttp.send(url)
	}
}

function onCustomChange(value, callback) {
	if(xmlHttp = GetXmlHttpObject()) {
		customer = value;
		var url = "query.php";
		url = url + "?board=" + board;
		url = url + "&customer=" + customer;
		url = url + "&type=business";
		url = url + "&sid=" + Math.random();
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
				document.getElementById("project").innerHTML = "";
				
				var sub = xmlHttp.responseText.split(',');
				var select_business = document.getElementById("select_business");
				select_business.options.length=0;
				var option = document.createElement("option");
				option.value = "empty";
				option.text = "请选择型号";
				select_business.add(option);
				for(i=0;i<sub.length;i++){
					var option = document.createElement("option");
					option.value = sub[i];
					option.text = sub[i];
					select_business.add(option);
				}
				$('#updateProject').hide();
			}
		};
		xmlHttp.open("GET", url, true);
		xmlHttp.send(url);
	}
}

function onBusinessChange(value, callback) {
	if(xmlHttp = GetXmlHttpObject()) {
		business = value;
		var url = "writeProduction.php";
		url = url + "?board=" + board;
		url = url + "&customer=" + customer;
		url = url + "&business=" + business;
		url = url + "&sid=" + Math.random();
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
				if(trimStr(xmlHttp.responseText) == 'failed' || trimStr(xmlHttp.responseText) == 'empty') {
					return;
				}
				document.getElementById("project").innerHTML = trimStr(xmlHttp.responseText);
				document.getElementById("date").value = getCurrDate();
				$('#updateProject').show();
			}
		};
		xmlHttp.open("GET", url, true);
		xmlHttp.send(url);
	}
}

function loadBoardData(callback) {
	if(xmlHttp = GetXmlHttpObject()) {
		var url = "query.php";
		url = url + "?type=board";
		url = url + "&sid=" + Math.random();
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
				
				var sub = xmlHttp.responseText.split(',');
				
				var select_board = document.getElementById("select_board");
				select_board.options.length=0;
				var option = document.createElement("option");
				option.value = "empty";
				option.text = "请选择主板";
				select_board.add(option);
				for(i=0;i<sub.length;i++){
					var option = document.createElement("option");
					option.value = sub[i];
					option.text = sub[i];
					select_board.add(option);
				}
				if(callback != null) callback();
			}
		};
		xmlHttp.open("GET", url, true);
		xmlHttp.send(url);
	}
}



/****************update project query****************/
function updateOnBoardChange(value, callback) {
	if(xmlHttp = GetXmlHttpObject()){
		board = value;
		var url = "query.php";
		url = url + "?board=" + board;
		url = url + "&type=customer";
		url = url + "&sid=" + Math.random();
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
				var sub = xmlHttp.responseText.split('|');
				var id = sub[0].split(',');
				var text = sub[1].split(',');
				var select_customer = document.getElementById("select_customer");
				select_customer.options.length=0;
				var option = document.createElement("option");
				option.value = "empty";
				option.text = "请选择客户";
				select_customer.add(option);
				
				var select_business = document.getElementById("select_business");
				select_business.options.length = 0;
				var option = document.createElement("option");
				option.value = "empty";
				option.text = "请选择客户";
				select_business.add(option);
				
				for(i=0;i<id.length;i++){
					var option = document.createElement("option");
					option.value = id[i];
					option.text = text[i];
					select_customer.add(option);
				}
				reset();
				if(callback != null) callback();
			}
		};
		xmlHttp.open("GET", url, true);
		xmlHttp.send(url)
	}
}

function updateOnCustomChange(value, callback) {
	if(xmlHttp = GetXmlHttpObject()) {
		customer = value;
		var url = "query.php";
		url = url + "?board=" + board;
		url = url + "&customer=" + customer;
		url = url + "&type=business";
		url = url + "&sid=" + Math.random();
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
				var sub = xmlHttp.responseText.split(',');
				var select_business = document.getElementById("select_business");
				select_business.options.length=0;
				var option = document.createElement("option");
				option.value = "empty";
				option.text = "请选择型号";
				select_business.add(option);
				for(i=0;i<sub.length;i++){
					var option = document.createElement("option");
					option.value = sub[i];
					option.text = sub[i];
					select_business.add(option);
				}
				reset();
				if(callback != null) callback();
			}
		};
		xmlHttp.open("GET", url, true);
		xmlHttp.send(url);
	}
}

/*
function updateOnBusinessChange(value, callback = null) {
	if(xmlHttp = GetXmlHttpObject()) {
		business = value;
		var url = "updateProject.php";
		url = url + "?board=" + board;
		url = url + "&customer=" + customer;
		url = url + "&business=" + business;
		url = url + "&sid=" + Math.random();
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
				document.getElementById("project").innerHTML = trimStr(xmlHttp.responseText);
				
				onProjectLoaded(board, customer, business);
			}
		};
		xmlHttp.open("GET", url, true);
		xmlHttp.send(url);
	}
}
*/

/******************proerpty query*********************/
function onPropertyTypeClick(type, callback) {
	if(xmlHttp = GetXmlHttpObject()) {
		var url = "queryProperty.php";
		url = url + "?type=" + type;
		url = url + "&sid=" + Math.random();
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
				document.getElementById("content").innerHTML = trimStr(xmlHttp.responseText);
				registAddBtnClickEvt();
			}
		};
		xmlHttp.open("GET", url, true);
		xmlHttp.send(url);
	}
}

/*
function initAutoCompleteData() {
	$('#search').autocomplete("autoCompleteQuery.php", {
			max: 12,    //列表里的条目数
			minChars: 0,    //自动完成激活之前填入的最小字符
			width: 400,     //提示的宽度，溢出隐藏
			scrollHeight: 300,   //提示的高度，溢出显示滚动条
			matchContains: true,    //包含匹配，就是data参数里的数据，是否只要包含文本框里的数据就显示
			autoFill: false,    //自动填充
			formatItem: function(row, i, max) {
				return i + '/' + max + ':"' + row.name + '"[' + row.to + ']';
			},
			formatMatch: function(row, i, max) {
				return row.name + row.to;
			},
			formatResult: function(row) {
				return row.to;
			}
		}).result(function(event, row, formatted) {
				alert(row.to);
		});
}
*/
